import { Exclude } from "class-transformer";
import { BeforeInsert, Column, Entity, JoinTable, ManyToMany, PrimaryGeneratedColumn } from "typeorm";
import * as bcrypt from "@node-rs/bcrypt";
import { Role } from "src/modules/role/entities/role.entity";
@Entity("user")
export class User {
  @PrimaryGeneratedColumn({ type: 'bigint' })
  id: string;

  @Column({ length: 55 })
  username: string;

  @Exclude()
  @Column({ length: 255, default: 'admin' })
  password: string;

  @ManyToMany(() => Role)
  @JoinTable({
    name: 'user_role',
    joinColumn: { name: 'user_id', referencedColumnName: 'id' },
    inverseJoinColumn: { name: 'role_id', referencedColumnName: 'id' },
  })
  roles: Role[];

  // 在数据库插入数据前，对密码进行加密
  @BeforeInsert()
  async encryptPwd() {
    this.password = await bcrypt.hash(this.password, 10);
  }
}